﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Semantics.Silverlight;

namespace SilverlightParser
{
    public partial class ParserWindow : ChildWindow
    {
        ParseResult ParseResult;
        LanguageNode LanguageNode;
        string words;
        ServiceReference1.ParserServiceClient client;
        public ParserWindow(ParseResult result,string words,ServiceReference1.ParserServiceClient client)
        {
            this.words = words;
            InitializeComponent();
            this.ParseResult = result;
            this.client = client;
        }
        
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //this.DialogResult = true;
            //string selectValue = this.languageNodeControl1.SelectedItem.ToString();
            string selectValue = ((ListItem)this.listBox1.SelectedItem).ID;

            int selectIndex=-1;
            selectIndex = this.listBox1.SelectedIndex;
            
            int chilidKey=this.ParseResult.Ambiguity.AmbiguousChild.PositionKey;
            if(selectIndex!=-1)
            {
               this.client.SelectProcedureCompleted += new EventHandler<ServiceReference1.SelectProcedureCompletedEventArgs>(client_SelectProcedureCompleted);
               this.client.SelectProcedureAsync(chilidKey, selectIndex, selectValue, words,ParseResult);
            }
            else
                MessageBox.Show("服务错错误!");
        }
        
        void client_SelectProcedureCompleted(object sender, ServiceReference1.SelectProcedureCompletedEventArgs e)
        {
            
            try
            {
                this.client.SelectProcedureCompleted -= new EventHandler<ServiceReference1.SelectProcedureCompletedEventArgs>(client_SelectProcedureCompleted);
                if (e.Result == null)
                {
                    MessageBox.Show("Parse failed with no result(reuslt=null)!");
                    return;
                }
                this.ParseResult= e.Result;
                switch (this.ParseResult.ResultState)
                {
                    case ParseResult.EnumResultState.Ambiguities:
                        this.Close();
                        ParserWindow win = new ParserWindow(this.ParseResult, this.words, this.client);
                        win.Show();
                        break;
                    case ParseResult.EnumResultState.Exception:
                        MessageBox.Show("服务异常!\r\n" + this.ParseResult.Exception);
                        break;
                    case ParseResult.EnumResultState.Success:
                        this.listBox1.Visibility = Visibility.Collapsed; 
                        this.languageNodeControl1.Items.Clear();
                        this.Title = "最终结果";
                        this.languageNodeControl1.ShowRootLangNode(this.ParseResult.ResultLangNode);
                        break;
                }
            }
            catch (System.Reflection.TargetInvocationException exp)
            {
                MessageBox.Show("服务错错误!");
                return;
            }
            catch (Exception exp)
           {
                
                MessageBox.Show(exp.Message, exp.GetType().ToString(), MessageBoxButton.OK);
            }
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string selectValue;
            string selectKey;
            if (this.ParseResult == null)
                return;
            if(this.ParseResult.ResultLangNode!=null)
                this.languageNodeControl1.ShowRootLangNode(this.ParseResult.ResultLangNode);
            else if (this.ParseResult.Ambiguity != null)
            {
                this.Title = this.ParseResult.Ambiguity.AmbiguousChild.Value;
                this.listBox1.Items.Clear();
                for(int i=0;i<this.ParseResult.Ambiguity.AmbiguousParents.Count;i++)
                {
                    LanguageNode AmbiguousParent=ParseResult.Ambiguity.AmbiguousParents[i];
                    selectValue =i+ ":"+AmbiguousParent.Value;
                    selectKey = AmbiguousParent.Value;
                    ListItem newitem = new ListItem(selectKey, selectValue);
                    this.listBox1.Items.Add(newitem);
                    this.languageNodeControl1.ShowRootLangNode(this.ParseResult.Ambiguity.AmbiguousParents[i]);
                }                
            }
        }

        private void languageNodeControl1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
        }
    }
}

